home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Franz PD / Franz PD Disk #047 (1990)(Amiga User Group Deutschland e.V.)[v Disaster Master 2].zip / Franz PD Disk #047 (1990)(Amiga User Group Deutschland e.V.)[v Disaster Master 2].adf / A68K_Beispiele / Prime.asm < prev    next >
Assembly Source File  |  1989-07-02  |  7KB  |  267 lines

  1. ***************************************
  2. *                                     *
  3. * Make a list of prime numbers. It    *
  4. * should work up to 4,294,967,295     *
  5. *                                     *
  6. * written by E. Lenz                  *
  7. *            Johann-Fichte-Strasse 11 *
  8. *            8 Munich 40              *
  9. *            Germany                  *
  10. *                                     *
  11. ***************************************
  12.  
  13.  
  14. _AbsExecBase     equ 4
  15.  
  16. *****EXEC*******
  17.  
  18. _LVOForbid       equ -$84
  19. _LVOPermit       equ -$8a
  20. _LVOAllocMem     equ -$c6
  21. _LVOFreeMem      equ -$d2
  22. _LVOGetMsg       equ -$174
  23. _LVOReplyMsg     equ -$17a
  24. _LVOWaitPort     equ -$180
  25. _LVOCloseLibrary equ -$19e
  26. _LVOOpenLibrary  equ -$228
  27.  
  28. *****DOS*****
  29.  
  30. _LVOOpen         equ -$1e
  31. _LVOClose        equ -$24
  32. _LVOWrite        equ -$30
  33.  
  34. TC_SIGRECVD      equ $1a
  35. pr_MsgPort       equ $5c
  36. pr_CLI           equ $ac
  37. ThisTask         equ $114
  38. VBlankFrequency  equ $212 
  39. MODE_OLDFILE     equ 1005
  40. MODE_NEWFILE     equ 1006
  41. SIGBREAK_ANY     equ $1000
  42.  
  43. ; INTERNALLY USED
  44. ;
  45. ; d3 buffer size
  46. ; d4 file handler
  47. ; d5 DosBase
  48. ; d6 console handler
  49. ; d7 buffer address
  50. ; a4 pointer to next entry in buffer
  51.  
  52.           move.l  d0,d4    save CLI parameters
  53.           movea.l a0,a4
  54.  
  55.           movea.l _AbsExecBase,a6   test if WB or CLI
  56.           movea.l ThisTask(a6),a0
  57.           lea     TC_SIGRECVD(a0),a1
  58.           move.l  a1,TaskSigs        get task signal address
  59.           moveq   #0,d0
  60.           tst.l   pr_CLI(a0)
  61.           bne.s   isCLI
  62.  
  63.           moveq   #1,d4             no parameter
  64.           lea     pr_MsgPort(a0),a0 for WB get WB Message
  65.           jsr     _LVOWaitPort(a6)
  66.           jsr     _LVOGetMsg(a6)
  67.  
  68. isCLI     move.l  d0,-(a7)
  69.           lea     window(pc),a3
  70.           cmpi.b  #60,VBlankFrequency(a6) test if PAL or NTSC
  71.           beq.s   isNTSC
  72.           movea.l a3,a0
  73.           addq.l  #8,a0
  74.           addq.l  #4,a0
  75.           move.l  #'256/',(a0)
  76.  
  77. isNTSC    move.l  #$6700,d0         allocate buffer
  78.           move.l  d0,d3             save buffer size
  79.           move.l  #$30000,d1        largest + clear
  80.           jsr     _LVOAllocMem(a6)
  81.           move.l  d0,d7
  82.           beq.s   exit
  83.  
  84.           lea     dosname(pc),a1    open DOS library
  85.           moveq   #0,d0
  86.           jsr     _LVOOpenLibrary(a6)
  87.           move.l  d0,d5
  88.           beq.s   exit
  89.  
  90.           movea.l d0,a6             open Console window
  91.           move.l  a3,d1
  92.           move.l  #MODE_OLDFILE,d2
  93.           jsr     _LVOOpen(a6)
  94.           move.l  d0,d6
  95.           beq.s   exit
  96.  
  97.           subq.l  #1,d4            open output file if named on 
  98.           beq.s   doPrime          command line
  99.           movea.l a4,a0
  100. find      cmpi.b  #$a,(a0)+
  101.           bne.s   find
  102.           clr.b   -(a0)
  103.           move.l  a4,d1
  104.           move.l  #MODE_NEWFILE,d2
  105.           jsr     _LVOOpen(a6)
  106.           move.l  d0,d4
  107.  
  108. ** start off with the prime number 3 ***
  109.  
  110. doPrime   movea.l d7,a4
  111.           moveq   #3,d0
  112.           move.l  d0,(a4)+
  113. prlop     bsr.s   ShowPrime
  114.           bsr     NextPrime
  115.  
  116.           movea.l TaskSigs(pc),a0
  117.           move.l  (a0),d1
  118.           andi.l  #SIGBREAK_ANY,d1
  119.           bne.s   exit
  120.  
  121.           tst.l   d0
  122.           bne.s   prlop
  123.  
  124. exit      tst.l   d5
  125.           beq.s   free
  126.           move.l  d6,d1             close console
  127.           beq.s   error
  128.           jsr     _LVOClose(a6)
  129.  
  130.           move.l  d4,d1             close file
  131.           beq.s   free
  132.           jsr     _LVOClose(a6)
  133.  
  134. free      movea.l _AbsExecBase,a6
  135.           tst.l   d7                free buffer
  136.           beq.s   error
  137.           movea.l d7,a1
  138.           move.l  d3,d0
  139.           jsr     _LVOFreeMem(a6)
  140.  
  141. error     move.l  (a7)+,d7
  142.           beq.s   NoBench
  143.           jsr     _LVOForbid(a6)    reply to WB
  144.           movea.l d7,a1
  145.           jsr     _LVOReplyMsg(a6)
  146.           jsr     _LVOPermit(a6)
  147.  
  148. NoBench   tst.l   d5
  149.           beq.s   nodos
  150.           movea.l d5,a1            close dos.lib
  151.           jsr     _LVOCloseLibrary(a6)
  152. nodos     moveq   #0,d0
  153.           rts
  154.  
  155. ********************************************
  156. *                                          *
  157. * Write number in d0 as decimal to console *
  158. *                                          *
  159. ********************************************
  160.  
  161.  
  162. ShowPrime movem.l d0-d3/a0-a1,-(a7)
  163.           lea     Prime(pc),a1
  164.           movea.l a1,a0
  165.           move.l  #'0000',d1
  166.           move.l  d1,(a1)+
  167.           move.l  d1,(a1)+
  168.           move.b  #$a,d1
  169.           move.l  d1,(a1)+
  170.           lea     Num(pc),a1
  171. plop      move.l  (a1)+,d1
  172.           addq.l  #1,a0
  173. pnext     cmp.l   d1,d0
  174.           bcs.s   plop
  175.           sub.l   d1,d0
  176.           addq.b  #1,(a0)
  177.           tst.l   d0
  178.           bne.s   pnext
  179.           lea     Prime(pc),a0
  180.           moveq   #12,d3
  181. ptest     cmpi.b  #'0',(a0)
  182.           bne.s   endp
  183.           subq.l  #1,d3
  184.           addq.l  #1,a0
  185.           bra.s   ptest
  186. endp      move.l  a0,d2
  187.           move.l  d6,d1
  188.           movem.l d2-d3,-(a7)
  189.           jsr     _LVOWrite(a6)
  190.           movem.l (a7)+,d2-d3
  191.           move.l  d4,d1
  192.           beq.s   nofile
  193.           jsr     _LVOWrite(a6)
  194.           tst.l   d0                  disk full?
  195.           bne.s   nofile
  196.           movem.l (a7)+,d0-d3/a0-a1
  197.           moveq   #-1,d0              end of programme
  198.           rts
  199. nofile    movem.l (a7)+,d0-d3/a0-a1
  200.           rts
  201.  
  202. ******************************************
  203. *                                        *
  204. * Calculate next prime number            *
  205. *                                        *
  206. ******************************************
  207.  
  208. NextPrime move.l  d7,a0
  209.           addq.l  #2,d0
  210.           bcs.s   eprim
  211. nlop      move.l  (a0)+,d2
  212.           beq.s   isPrime
  213.           move.l  d2,d1
  214.           mulu    d2,d1
  215.           cmp.l   d1,d0
  216.           bcs.s   isPrime
  217.           bsr.s   mod
  218.           tst.l   d2
  219.           beq.s   NextPrime     no prime - try next number
  220.           bra.s   nlop          try next divisor
  221. isPrime   move.l  d0,d1
  222.           swap    d1
  223.           tst.w   d1
  224.           bne.s   nosave
  225.           move.l  d0,(a4)+
  226. nosave    rts
  227. eprim     moveq   #0,d0
  228.           rts
  229.  
  230. **************************
  231. *                        *
  232. * return d0 mod d2 in d2 *
  233. *                        *
  234. **************************
  235.  
  236. mod       move.l  d0,-(a7)
  237.           lea     pow(pc),a1
  238.           moveq   #9,d1
  239. mlop      move.l  d2,(a1)+
  240.           add.l   d2,d2
  241.           dbra    d1,mlop
  242. mnext     move.l  -(a1),d1
  243.           beq.s   mend
  244. mwhat     cmp.l   d1,d0
  245.           bcs.s   mnext
  246.           sub.l   d1,d0
  247.           beq.s   mend
  248.           bra.s   mwhat
  249. mend      move.l  d0,d2
  250.           move.l  (a7)+,d0
  251.           rts
  252.  
  253. TaskSigs  ds.l 1
  254. Num       dc.l 1000000000,100000000,10000000,1000000,100000,10000,1000,100,10,1
  255. Prime     ds.b 12
  256.  
  257.           dc.l 0
  258. pow       ds.l 1
  259.  
  260. dosname   dc.b 'dos.library',0
  261.           even
  262.  
  263. window    dc.b 'CON:0/0/200/200/Prime numbers',0
  264.           even
  265.           end
  266.  
  267.